using System.Linq;
using Godot;
using GoGameClient.script.resources;

namespace GoGameClient.script.role;

public partial class Model : AnimatedSprite2D
{
    // Called when the node enters the scene tree for the first time.
    private int _height = 98;
    private Vector2 _position;
    

    public void SetModel(string name)
    {
        SpriteFrames = new SpriteResources(name).GetSpriteFrames();
        var frameTexture = SpriteFrames.GetFrameTexture(SpriteFrames.GetAnimationNames()[0], 1);
        var height = frameTexture.GetHeight();
        if (_position == new Vector2(0, 0))
        {
            _position = Position;
        }

        if (_height >= height)
        {
            height = (_height - height) / 2;
            Position = new Vector2(_position.X, _position.Y + height);
        }
        else
        {
            height = (height - _height) / 4;
            Position = new Vector2(_position.X, _position.Y - height);
        }

        Animation = SpriteFrames.GetAnimationNames()[0];
        foreach (var animationName in SpriteFrames.GetAnimationNames())
        {
            if (!"站立-下".Equals(animationName)) continue;
            Animation = animationName;
            Play(animationName);
        }
    }

    public void Move(string direction)
    {
        Play($"行走-{direction}");
    }

    public void Wait(string direction)
    {
        Play($"站立-{direction}");
    }

    public void SetAction(string action)
    {
        //根据动作名称，从SpriteFrames查询相关动画名称
        var findIndex = SpriteFrames.GetAnimationNames().ToList().FindIndex((item) => item.Equals(action));
        if (findIndex == -1) return;
        Play(action);
    }
}